package pers.amos.eshop.request;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author amos wong
 * @create 2020-11-14 3:28 下午
 * <p>
 * 内存中的请求队列
 */

public class RequestQueue {

    /**
     * 内存队列，包含多个阻塞队列
     */
    private List<ArrayBlockingQueue<Request>> queues = new ArrayList<>();

    /**
     * 标识map，用于优化重复读请求的
     */
    private Map<Long, Boolean> flagMap = new ConcurrentHashMap<>();

    /**
     * 静态内部类实现单例模式
     */
    private static class Singleton {

        private static RequestQueue instance;

        static {
            instance = new RequestQueue();
        }

        public static RequestQueue getInstance() {
            return instance;
        }
    }

    public static RequestQueue getInstance() {
        return Singleton.getInstance();
    }

    public void addQueue(ArrayBlockingQueue<Request> queue) {
        this.queues.add(queue);
    }

    public Integer getQueueSize() {
        return queues.size();
    }

    public ArrayBlockingQueue<Request> getQueue(int index) {
        return queues.get(index);
    }

    public Map<Long, Boolean> getFlagMap() {
        return this.flagMap;
    }
}
